// pages/item/item.js
const http = require("../../utils/http.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        itemObj: {},
        gaiyaotype: true,
        xiangxitype: false,
        jiagetype: false,
        gaiyaoTop: 0,
        xiangxiTop: 0,
        jiageTop: 0,
        type: false
    },
    // 返回首页
    goHome() {
        wx.navigateBack()
    },
    // 拨打电话
    phonecall() {
        wx.makePhoneCall({
            phoneNumber: '12345678910',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        http.request("product/product/getProductDetails/" + options.id, "", "get").then(res => {
            console.log(res);
            this.setData({
                itemObj: res.data
            });

            wx.createSelectorQuery().select("#gaiyao").boundingClientRect(rect => {
                this.setData({
                    gaiyaoTop: rect.top
                })
            }).exec();
            wx.createSelectorQuery().select("#xiangxi").boundingClientRect(rect => {
                this.setData({
                    xiangxiTop: rect.top
                })
            }).exec();
            wx.createSelectorQuery().select("#jiage").boundingClientRect(rect => {
                this.setData({
                    jiageTop: rect.top
                })
            }).exec();
        })
    },
    //页面滚动时触发的钩子函数
    onPageScroll(ev) {
        // console.log("滚", ev);
        const top = ev.scrollTop + 50;
        if (top >= this.data.gaiyaoTop) {
            this.setData({
                type: true
            })
        } else {
            this.setData({
                type: false
            })
        }
        const {
            gaiyaotype,
            xiangxiTop,
            jiageTop,
            gaiyaoTop,
            xiangxitype,
            jiagetype
        } = this.data;
        if (top >= gaiyaotype && top < xiangxiTop) {
            this.setData({
                gaiyaotype: true,
                xiangxitype: false,
                jiagetype: false
            })
        } else if (top >= xiangxiTop && top < jiageTop) {
            this.setData({
                gaiyaotype: false,
                xiangxitype: true,
                jiagetype: false
            })
        } else {
            this.setData({
                gaiyaotype: false,
                xiangxitype: false,
                jiagetype: true
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})